package test.freshui.ffmpeg;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

class PlayerView extends View {
	private final String TAG = "playerView";
    private Bitmap mBitmap = null;
    private long mStartTime;
    
    public PlayerView(Context context){
    	super(context);
    	Log.d(TAG, "playerView(Context context)");
    	//init();
    }
    

    public PlayerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        
        Log.d(TAG, "playerView(Context context, AttributeSet attrs)");
        //final int W = 200;
        //final int H = 200;

        //init(200, 200);
    }

    public void init(int width, int height){
    	Log.d(TAG, "width="+width+",height="+height);
    	if(mBitmap != null){
    		mBitmap.recycle();
    		mBitmap = null;
    	}
        mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        mStartTime = System.currentTimeMillis();
        nativeViewInit(mBitmap);   	
    }
    public Bitmap getBitmap(){
    	return mBitmap;
    }
    
    @Override protected void onDraw(Canvas canvas) {
    	//Log.d(TAG, "onDraw() width="+getWidth()+", height="+getHeight());
        //canvas.drawColor(0xFFCCCCCC);
        //renderPlasma(mBitmap, System.currentTimeMillis() - mStartTime);
    	if(mBitmap != null && nativeViewRender(mBitmap) >= 0){
    		canvas.drawBitmap(mBitmap, 0, 0, null);
    		Log.d(TAG, "drawBitmap"+mBitmap);
    	} else {
    		//Log.d(TAG, "not draw! not ready!");
    	}
    	
    	//nativeViewRender();
        // force a redraw, with a different time-based pattern.
        try {
			Thread.sleep(30); // 25 fps.
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        invalidate();
		
    	super.onDraw(canvas);
    }
    
    private native int nativeViewInit(Bitmap mBitmap);
    private native int nativeViewRender(Bitmap mBitmap);
}